/***************************************************************************
 *   Copyright (C) 2008 by Goliash   *
 *   goliash@goliash.net   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "rename_medium_dialog.h"

RenameMediumDialog::RenameMediumDialog(int id, QWidget *parent)
{
	this->id = id;
	//QDialog *renameMediumDialog = new QDialog(this);
	QVBoxLayout *vboxLayout = new QVBoxLayout(this);
	QLabel *dialogText = new QLabel(tr("Enter your new medium name:"));
	entry = new QLineEdit();
	QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
		
	connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
	connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
		
	vboxLayout->addWidget(dialogText);
	vboxLayout->addWidget(entry);
	vboxLayout->addWidget(buttons);
	//this->exec();
}

void RenameMediumDialog::accept()
{
	newName = entry->text();
	rename();
	this->close();
}

void RenameMediumDialog::rename()
{
	qDebug() << id << newName;

		QSqlQuery query;
		query.prepare("UPDATE media SET name=:name WHERE id=:id");
		query.bindValue(":id", id);
		query.bindValue(":name", newName);
		query.exec();
		query.finish();
}
